//
//  SourceCatogryViewController.swift
//  test
//
//  Created by colin on 2019/3/29.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit
import Then

class SourceCatogryViewController: ScrollViewController {
    
    private var audios = [CLAudio]()
    private lazy var infoTableView = UITableView(frame: CGRect.zero, style: .plain)
    private var headerHeight:CGFloat = 0
    
    convenience init(data:JSONObject?,headerHeight:CGFloat) {
        self.init()
        self.audios = CLAudio.audios(objects: data?["goodsList"] as? [JSONObject])
        self.headerHeight = headerHeight
    }
    
    override func scrollView() -> UIScrollView? {
        return self.infoTableView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        self.infoTableView.do {
            $0.showsVerticalScrollIndicator = false
            $0.sectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.sectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.estimatedSectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
            $0.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
            $0.contentInsetAdjustmentBehavior = .never
            $0.register(Cell.self, forCellReuseIdentifier: Cell.reuseId)
            $0.delegate = self
            $0.dataSource = self
            
            $0.contentInset = UIEdgeInsets(top: self.headerHeight, left: 0, bottom: 0, right: 0)
            $0.contentOffset = CGPoint(x: 0, y: -self.headerHeight)
        }
        self.view.addSubview(self.infoTableView)
        self.infoTableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
    }
}

// MARK: - UITableViewDataSource
extension SourceCatogryViewController : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.audios.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Cell.reuseId) as! Cell
        cell.refresh(audio: self.audios[indexPath.row], index: indexPath.row)
        return cell
    }
    
}

// MARK: - UITableViewDelegate
extension SourceCatogryViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  Cell.height
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        // 进入播放页面
        let controller = AudioPlayViewController()
        self.show(controller, sender: nil)
        
        // 开始播放
        controller.play(audios: self.audios, index: indexPath.row)
        
        tableView.deselectRow(at: indexPath, animated: true)
        
    }
}

extension SourceCatogryViewController {
    
    class Cell: UITableViewCell {
        
        static let reuseId = "Cell"
        static let height:CGFloat = 70.0
        
        private lazy var imageViewPlay = UIImageView()
        private lazy var labelTitle = UILabel()
        private lazy var labelPlayCount = UILabel()
        private lazy var labelTime = UILabel()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            
            self.imageViewPlay.do {
                $0.image = UIImage(named: "icon_list_play")
                $0.contentMode = .center
            }
            self.contentView.addSubview(self.imageViewPlay)
            self.imageViewPlay.snp.makeConstraints { (make) in
                make.left.top.bottom.equalToSuperview()
                make.width.equalTo(self.contentView.snp.height)
            }
            
            self.labelTitle.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 16.0, weight: .regular)
            }
            self.contentView.addSubview(self.labelTitle)
            self.labelTitle.snp.makeConstraints { (make) in
                make.left.equalTo(self.imageViewPlay.snp.right)
                make.top.equalToSuperview().offset(10.0)
                make.right.equalToSuperview().offset(-15.0)
            }
            
            self.labelPlayCount.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 13.0, weight: .light)
            }
            self.contentView.addSubview(self.labelPlayCount)
            self.labelPlayCount.snp.makeConstraints { (make) in
                make.left.equalTo(self.imageViewPlay.snp.right)
                make.bottom.equalToSuperview().offset(-10.0)
            }
            
            self.labelTime.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 13.0, weight: .light)
            }
            self.contentView.addSubview(self.labelTime)
            self.labelTime.snp.makeConstraints { (make) in
                make.left.equalTo(self.labelPlayCount.snp.right).offset(10.0)
                make.bottom.equalToSuperview().offset(-10.0)
            }
        }
        
        func refresh(audio:CLAudio,index:Int) {
            self.labelTitle.text = audio.title
            self.labelPlayCount.text = "播放量: \(audio.play_count)"
            self.labelTime.text = "时长: \(audio.play_time.hms)"
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}

// MARK: - UIScrollViewDelegate
extension SourceCatogryViewController : UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.scrollHandle?(self.index,scrollView)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            self.endDecelerating?(self.index,scrollView)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.endDecelerating?(self.index,scrollView)
    }
}
